﻿#pragma once

#include"RBTree.h"

namespace bit
{
	template<class K, class V>
	class map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::Iterator iterator;
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::ConstIterator const_iterator;

		iterator begin()
		{
			return _rbtree.Begin();
		}

		iterator end()
		{
			return _rbtree.End();
		}

		const_iterator begin() const
		{
			return _rbtree.Begin();
		}

		const_iterator end() const
		{
			return _rbtree.End();
		}

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _rbtree.Insert(kv);//调用Insert函数，此时所有模板类型都已经明确，开始插入
		}

		// 17:00
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = _rbtree.Insert({ key, V() });
			return ret.first->second;
		}

		iterator find(const K& key)
		{
			return _rbtree.Find(key);
		}

	private:
		RBTree<K, pair<const K, V>, MapKeyOfT> _rbtree;//实例化_rbtree对象后，可以使用其内部的成员变量和函数，模板类型为 int，int，MapKeyOfT
	};

	void test_map()
	{
		map<int, int> m;//数据类型为 int int
		m.insert({ 1,1 });
		m.insert({ 5,5 });
		m.insert({ 2,2 });
		m.insert({ 6,6 });

		map<int, int>::iterator it = m.begin();
		while (it != m.end())
		{
			//it->first += 1;
			it->second += 1;

			cout << it->first << ":" << it->second << endl;
			++it;
		}

		map<string, string> dict;
		dict.insert({ "sort", "" });
		dict.insert({ "left", "" });
		dict.insert({ "right", "ұ" });
		dict["left"] = "ߣʣ";
		dict["insert"] = "";
		dict["string"];

		for (auto& kv : dict)
		{
			cout << kv.first << " " << kv.second << endl;
		}
		cout << endl;

		string arr[] = { "ƻ", "", "ƻ", "", "ƻ", "ƻ", "",
	"ƻ", "㽶", "ƻ", "㽶" };

		map<string, int> countMap;
		for (const auto& str : arr)
		{
			countMap[str]++;
		}

		for (const auto& e : countMap)
		{
			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;
	}
}